﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using PRT;

public partial class editclients : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void btSearch_Click(object sender, EventArgs e)
    {
        dsClient.Where = "Name.Contains(@Search) || Number.Contains(@Search)";
        dsClient.WhereParameters.Add(new Parameter("Search", DbType.String, tbSearch.Text));
        gvClient.DataBind();
    }

    protected void btClearSearch_Click(object sender, EventArgs e)
    {
        dsClient.Where = "";
        dsClient.WhereParameters.Clear();
        tbSearch.Text = "";
        gvClient.DataBind();
    }

    protected void LinqDataSource_Selecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        foreach (KeyValuePair<string, object> kvp in e.WhereParameters)
        {
            if (kvp.Value == null)
            {
                e.Cancel = true;
                return;
            }
        }
    }
    
    protected void btClientInsert_Click(object sender, EventArgs e)
    {
        TextBox tbName = (TextBox)gvClient.FooterRow.FindControl("tbName_Insert");
        TextBox tbNumber = (TextBox)gvClient.FooterRow.FindControl("tbNumber_Insert");
        TextBox tbExtension = (TextBox)gvClient.FooterRow.FindControl("tbExtension_Insert");
        DropDownList ddClientType = (DropDownList)gvClient.FooterRow.FindControl("ddClientType_Insert");
        TextBox tbWebsite = (TextBox)gvClient.FooterRow.FindControl("tbWebsite_Insert");
        AnsweringServiceDataContext db = new AnsweringServiceDataContext();
        Client c = new Client();
        c.Name = tbName.Text;
        c.Number = tbNumber.Text;
        c.Extension = tbExtension.Text;
        if (ddClientType.SelectedValue != "")
            c.ClientTypeID = Convert.ToByte(ddClientType.SelectedValue);
        c.Website = tbWebsite.Text;

        db.Clients.InsertOnSubmit(c);
        db.SubmitChanges();
        gvClient.DataBind();
    }

    protected void gvClientNumbers_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        fvClientNumbers.PageIndex = e.NewSelectedIndex;
    }

    protected void btClientNumberInsert_Click(object sender, EventArgs e)
    {
        TextBox tbNumber = (TextBox)gvEditClientNumbers.FooterRow.FindControl("tbNumber_Insert");
        TextBox tbDesc = (TextBox)gvEditClientNumbers.FooterRow.FindControl("tbDesc_Insert");
        DropDownList ddNumberType = (DropDownList)gvEditClientNumbers.FooterRow.FindControl("ddNumberType_Insert");
        DropDownList ddProvider = (DropDownList)gvEditClientNumbers.FooterRow.FindControl("ddProvider_Insert");
        CheckBox cbDID = (CheckBox)gvEditClientNumbers.FooterRow.FindControl("cbDID_Insert");
        
        AnsweringServiceDataContext db = new AnsweringServiceDataContext();
        Number n = new Number();
        n.ClientID = (short)gvClient.SelectedValue;
        n.NumberTypeID = Convert.ToByte(ddNumberType.SelectedValue);
        n.Description = tbDesc.Text;
        n.Number1 = tbNumber.Text.Replace("-","");
        if (ddProvider.SelectedValue != "")
            n.ProviderID = Convert.ToByte(ddProvider.SelectedValue);
        n.DID = cbDID.Checked;

        db.Numbers.InsertOnSubmit(n);
        db.SubmitChanges();
        gvEditClientNumbers.DataBind();
    }

    protected void btClientEmailInsert_Click(object sender, EventArgs e)
    {
        TextBox tbDescription = (TextBox)gvClientEmail.FooterRow.FindControl("tbDescription_Insert");
        TextBox tbAddress = (TextBox)gvClientEmail.FooterRow.FindControl("tbAddress_Insert");

        AnsweringServiceDataContext db = new AnsweringServiceDataContext();
        Email a = new Email();
        a.ClientID = (short)gvClient.SelectedValue;
        a.Description = tbDescription.Text;
        a.Address = tbAddress.Text;

        db.Emails.InsertOnSubmit(a);
        db.SubmitChanges();
        gvClientEmail.DataBind();
    }

    protected void gvClient_SelectedIndexChanged(object sender,  EventArgs e)
    {
        //
        gvEditClientNumbers.SelectedIndex = 0;
        gvEditClientNumbers.ShowFooterWhenEmpty = true;
        gvClientEmail.ShowFooterWhenEmpty = true;
        btEditGreeting.Visible = true;
        btEditInstructions.Visible = true;
        dvSpecificInstructions.Visible = true;
        gvCustomFields.ShowFooterWhenEmpty = true;
        
        //Get client info for selected row in gvClient
        AnsweringServiceDataContext db = new AnsweringServiceDataContext();
        Client client = (from c in db.Clients
                where c.ClientID == (short)gvClient.SelectedValue
                select c).Single();
        
        //Set fields in Greeting
        hfGreetingClientID.Value = client.ClientID.ToString();
        if (client.Greeting != null)
            lbGreeting.Text = client.Greeting.Replace("\r\n", "<br>");
        else
            lbGreeting.Text = "";
        tbEditGreeting.Text = client.Greeting;
        
        //Set fields in General Instructions
        hfInstructionsClientID.Value = client.ClientID.ToString();
        if (client.Instructions != null)
            lbInstructions.Text = client.Instructions.Replace("\r\n", "<br>");
        else
            lbInstructions.Text = "";
        tbEditInstructions.Text = client.Instructions;
    }

    protected void btUpdateGreeting_Click(object sender, EventArgs e)
    {
        AnsweringServiceDataContext db = new AnsweringServiceDataContext();

        var client = (from c in db.Clients
                      where c.ClientID == Convert.ToInt32(hfGreetingClientID.Value)
                      select c).First();
        client.Greeting = tbEditGreeting.Text;
        db.SubmitChanges();
        lbGreeting.Text = Server.HtmlEncode(tbEditGreeting.Text);
    }

    protected void btUpdateInstructions_Click(object sender, EventArgs e)
    {
        AnsweringServiceDataContext db = new AnsweringServiceDataContext();

        var client = (from c in db.Clients
                      where c.ClientID == Convert.ToInt32(hfInstructionsClientID.Value)
                      select c).First();
        client.Instructions = tbEditInstructions.Text;
        db.SubmitChanges();
        lbInstructions.Text = tbEditInstructions.Text;
    }


    protected void btNoSpecificInstructions_Click(object sender, EventArgs e)
    {
        dvSpecificInstructions.ChangeMode(DetailsViewMode.Insert);
    }

    protected void btCustomFieldInsert_Click(object insert, EventArgs e)
    {
        TextBox tbFieldName = (TextBox)gvCustomFields.FooterRow.FindControl("tbFieldName_Insert");
        DropDownList ddFieldColumn = (DropDownList)gvCustomFields.FooterRow.FindControl("ddFieldColumn_Insert");
        AnsweringServiceDataContext db = new AnsweringServiceDataContext();

        CustomField field = new CustomField();
        field.ClientID = (short)gvClient.SelectedValue;
        field.Name = tbFieldName.Text;
        field.FieldColumnID = Convert.ToByte(ddFieldColumn.SelectedValue);
        db.CustomFields.InsertOnSubmit(field);
        db.SubmitChanges();
        gvCustomFields.DataBind();
    }
}


